package city.spring.modules.oldman.controller;

import city.spring.modules.oldman.entity.OldmanComplaintEntity;
import city.spring.modules.oldman.service.OldmanComplaintService;
import city.spring.modules.oldman.vo.OldmanComplaintVO;
import city.spring.utils.MyBatisUtils;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.support.SFunction;
import com.baomidou.mybatisplus.extension.api.ApiController;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.data.web.SortDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

/**
 * Controller：投诉
 *
 * @author pxm
 * @date 2020-06-22 10:43:20
 */
@RestController
@RequestMapping("/oldman_complaint")
public class OldmanComplaintController extends ApiController {
    private final OldmanComplaintService oldmanComplaintService;
    /**
     * 实体类可排序字段
     */
    private final List<SFunction<OldmanComplaintEntity, ?>> entityOrderFields;

    public OldmanComplaintController(OldmanComplaintService oldmanComplaintService) {
        this.oldmanComplaintService = oldmanComplaintService;
        entityOrderFields = new ArrayList<>();
        entityOrderFields.add(OldmanComplaintEntity::getId);
        entityOrderFields.add(OldmanComplaintEntity::getComplainant);
        entityOrderFields.add(OldmanComplaintEntity::getType);
        entityOrderFields.add(OldmanComplaintEntity::getHappenTime);
        entityOrderFields.add(OldmanComplaintEntity::getPhone);
        entityOrderFields.add(OldmanComplaintEntity::getRelation);
        entityOrderFields.add(OldmanComplaintEntity::getOldman);
        entityOrderFields.add(OldmanComplaintEntity::getContent);
        entityOrderFields.add(OldmanComplaintEntity::getOrg);
        entityOrderFields.add(OldmanComplaintEntity::getSound);
        entityOrderFields.add(OldmanComplaintEntity::getHandle);
        entityOrderFields.add(OldmanComplaintEntity::getHandleTime);
        entityOrderFields.add(OldmanComplaintEntity::getHandleRemarks);
        entityOrderFields.add(OldmanComplaintEntity::getReview);
        entityOrderFields.add(OldmanComplaintEntity::getReviewer);
        entityOrderFields.add(OldmanComplaintEntity::getReviewTime);
        entityOrderFields.add(OldmanComplaintEntity::getReviewRemarks);
        entityOrderFields.add(OldmanComplaintEntity::getGmtCreate);
        entityOrderFields.add(OldmanComplaintEntity::getGmtModified);
    }

    /**
     * 获取全部的 <strong>投诉</strong> 列表
     */
    @GetMapping("all")
    public Object listAll(@PageableDefault(sort = {"gmtCreate"}) Pageable pageable, OldmanComplaintVO entity) {
        LambdaQueryChainWrapper<OldmanComplaintEntity> lambdaQuery = buildLambdaQuery(entity);
        MyBatisUtils.lambdaQueryAddOrder(lambdaQuery, pageable, entityOrderFields);
        return success(lambdaQuery.list());
    }

    /**
     * 分页获取 <strong>投诉</strong> 列表
     *
     * @param pageable 分页参数信息
     */
    @GetMapping
    public Object list(@PageableDefault @SortDefault(sort = "happen_time", direction = Sort.Direction.DESC) Pageable pageable, OldmanComplaintVO entity) {
        QueryWrapper<? extends OldmanComplaintEntity> queryWrapper = buildQuery(entity);
        return success(oldmanComplaintService.listPage(MyBatisUtils.toPage(pageable), queryWrapper));
    }

    /**
     * 获取一个 <strong>投诉</strong>
     *
     * @param id 主键ID
     */
    @GetMapping("{id}")
    public Object info(@PathVariable String id) {
        return success(oldmanComplaintService.getComplaint(id));
    }

    /**
     * 添加一个 <strong>投诉</strong>
     *
     * @param entity 修改后的信息
     */
    @PostMapping
    public Object add(@RequestBody OldmanComplaintEntity entity) {
        oldmanComplaintService.saveOldmanComplaint(entity);
        return success(entity);
    }

    /**
     * 修改一个 <strong>投诉</strong>
     *
     * @param id     主键ID
     * @param entity 修改后的信息
     */
    @PutMapping("{id}")
    public Object update(@PathVariable String id, @RequestBody OldmanComplaintEntity entity) {
        entity.setId(id);
        oldmanComplaintService.updateOldmanComplaint(entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 更新处理状态
     *
     * @param id
     * @param entity
     * @return
     */
    @PutMapping("/handle/{id}")
    public Object updateHandle(@PathVariable String id, @RequestBody OldmanComplaintEntity entity) {
        oldmanComplaintService.updateHandle(id, entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 更新回访状态
     *
     * @param id
     * @return
     */
    @PutMapping("/review/{id}")
    public Object updateReview(@PathVariable String id, @RequestBody OldmanComplaintEntity entity) {
        oldmanComplaintService.updateReview(id, entity);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    /**
     * 删除一个 <strong>投诉</strong>
     *
     * @param id 主键ID
     */
    @DeleteMapping("{id}")
    public Object delete(@PathVariable String id) {
        oldmanComplaintService.deleteOldmanComplaint(id);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 删除多个 <strong>投诉</strong>
     *
     * @param ids 主键ID列表
     */
    @DeleteMapping
    public Object deleteIds(@RequestBody List<String> ids) {
        oldmanComplaintService.deleteOldmanComplaint(ids);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private LambdaQueryChainWrapper<OldmanComplaintEntity> buildLambdaQuery(OldmanComplaintVO entity) {
        LambdaQueryChainWrapper<OldmanComplaintEntity> lambdaQuery = oldmanComplaintService.lambdaQuery();
        lambdaQuery.eq(StringUtils.isNotBlank(entity.getId()), OldmanComplaintEntity::getId, entity.getId());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getComplainant()), OldmanComplaintEntity::getComplainant, entity.getComplainant());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getType()), OldmanComplaintEntity::getType, entity.getType());
        lambdaQuery.eq(entity.getHappenTime() != null, OldmanComplaintEntity::getHappenTime, entity.getHappenTime());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getPhone()), OldmanComplaintEntity::getPhone, entity.getPhone());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getRelation()), OldmanComplaintEntity::getRelation, entity.getRelation());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getOldman()), OldmanComplaintEntity::getOldman, entity.getOldman());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getContent()), OldmanComplaintEntity::getContent, entity.getContent());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getOrg()), OldmanComplaintEntity::getOrg, entity.getOrg());
        lambdaQuery.like(StringUtils.isNotBlank(entity.getSound()), OldmanComplaintEntity::getSound, entity.getSound());
        lambdaQuery.eq(entity.getHandle() != null, OldmanComplaintEntity::getHandle, entity.getHandle());
        lambdaQuery.eq(entity.getReview() != null, OldmanComplaintEntity::getReview, entity.getReview());
        lambdaQuery.eq(entity.getGmtCreate() != null, OldmanComplaintEntity::getGmtCreate, entity.getGmtCreate());
        lambdaQuery.eq(entity.getGmtModified() != null, OldmanComplaintEntity::getGmtModified, entity.getGmtModified());
        return lambdaQuery;
    }

    /**
     * 构建查询条件内容
     *
     * @param entity 实体对象
     * @return lambda query chain wrapper
     */
    private QueryWrapper<OldmanComplaintEntity> buildQuery(OldmanComplaintVO entity) {
        QueryWrapper<OldmanComplaintEntity> wrapper = new QueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(entity.getId()), "id", entity.getId());
        wrapper.like(StringUtils.isNotBlank(entity.getComplainant()), "t1.complainant", entity.getComplainant());
        wrapper.like(StringUtils.isNotBlank(entity.getType()), "t1.type", entity.getType());
        wrapper.eq(entity.getHappenTime() != null, "t1.happen_time", entity.getHappenTime());
        wrapper.like(StringUtils.isNotBlank(entity.getPhone()), "t1.phone", entity.getPhone());
        wrapper.like(StringUtils.isNotBlank(entity.getRelation()), "t1.relation", entity.getRelation());
        wrapper.like(StringUtils.isNotBlank(entity.getOldman()), "t1.oldman", entity.getOldman());
        wrapper.like(StringUtils.isNotBlank(entity.getOldmanName()), "t2.oldman_name", entity.getOldmanName());
        wrapper.like(StringUtils.isNotBlank(entity.getCertificate()), "t2.certificate", entity.getCertificate());
        wrapper.like(StringUtils.isNotBlank(entity.getTelephone()), "t2.telephone", entity.getTelephone());
        wrapper.like(StringUtils.isNotBlank(entity.getContent()), "t1.content", entity.getContent());
        wrapper.like(StringUtils.isNotBlank(entity.getOrg()), "t1.org", entity.getOrg());
        wrapper.like(StringUtils.isNotBlank(entity.getHandleRemarks()), "t1.handle_remarks", entity.getHandleRemarks());
        wrapper.like(StringUtils.isNotBlank(entity.getReviewRemarks()), "t1.review_remarks", entity.getReviewRemarks());
        wrapper.like(StringUtils.isNotBlank(entity.getSound()), "t1.sound", entity.getSound());
        wrapper.eq(entity.getHandle() != null, "t1.handle", entity.getHandle());
        wrapper.eq(entity.getHandleTime() != null, "t1.handle_time", entity.getHandleTime());
        wrapper.eq(entity.getReview() != null, "t1.review", entity.getReview());
        wrapper.eq(entity.getReviewTime() != null, "t1.review_time", entity.getReviewTime());
        wrapper.eq(entity.getGmtCreate() != null, "t1.gmt_create", entity.getGmtCreate());
        wrapper.eq(entity.getGmtModified() != null, "t1.gmt_modified", entity.getGmtModified());
        return wrapper;
    }

}
